import { Sqlit3DB } from "../TeSqlite3";
import { ifBase, Tables, ifKeepKeyUnit, ifKeepImageUnit } from './base';
// Sqlit3DB.registTable(Tables.user, {
//     "mail": {
//         "type": "string",
//         "isPrimary": true
//     },
//     "key": "string",
//     "count": "number",
//     "time": "number"
// });
Sqlit3DB.registTable(Tables.tinypic, {
    "srcmd5": {
        "type": "string",
        "isPrimary": true
    },
    "name": "string",
    "tinyMd5": "string",
    "time": "number",
    "pic": "Buffer"
});
export class SqliteMode implements ifBase {
    init(path: string) {
        return Sqlit3DB.inst.init(path)
    }
    // change(this: ifKeepKeyUnit, nCount: number) {
    //     this.count = nCount;
    //     return Sqlit3DB.inst.updateInfo(Tables.user, { key: this.key }, { count: this.count });
    // }
    // async getPressKey() {
    //     let result = await Sqlit3DB.inst.selectInfo(Tables.user, { 'count': { '$gt': 0 } });
    //     if (result.length == 0)
    //         throw 'no account';
    //     return { count: result[0].count, key: result[0].key, change: this.change };
    // }

    save(this: ifKeepImageUnit, key: string, value: any, ...args: any) {
        let info = {};
        for (let i = 0; i < arguments.length; i = i + 2) {
            info[arguments[i]] = arguments[i + 1];
        }
        return Sqlit3DB.inst.updateInfo(Tables.tinypic, { srcmd5: this.srcmd5 }, info);
    }

    async removeSelf(this: ifKeepImageUnit) {
        await Sqlit3DB.inst.deleteInfo(Tables.tinypic, { srcmd5: this.srcmd5 });
        return true;
    }

    async getPic(f: any) {
        let result = await Sqlit3DB.inst.selectInfo(Tables.tinypic, f);
        if (result.length == 0) return undefined;
        return { tinyMd5: result[0].tinyMd5, srcmd5: result[0].srcmd5, time: result[0].time, name: result[0].name, pic: result[0].pic, save: this.save, removeSelf: this.removeSelf } as ifKeepImageUnit;
    }

    async inserPic(f: any) {
        return Sqlit3DB.inst.insertInfo(Tables.tinypic, f);
    }

    freshAllDB() {
        // 这里就等3s吧
        return new Promise<boolean>(function (r) {
            setTimeout(function () {
                r(true)
            }, 1000)
        })
    }
}
